# -*- coding: utf-8; mode: Python -*-
# SConstruct for ra/test

# (c) Daniel Llorens - 2015-2023
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.

# FIXME Shared pieces with {examples,bench,docs}/SConstruct

import os, string, atexit
from os.path import join, abspath
import imp
ra = imp.load_source('ra', '../config/ra.py')

# FIXME pick BLAS flags from here.
try:
    Import('top')
except:
    top = {}

vars = Variables()
vars.AddVariables(PathVariable('variant_dir', 'build directory', '.', PathVariable.PathIsDirCreate))

env = Environment(options=vars,
                  ENV=dict([(k, os.environ[k] if k in os.environ else '')
                            for k in ['PATH', 'HOME', 'TERM', 'LD_RUN_PATH', 'DYLD_LIBRARY_PATH',
                                      'RPATH', 'LIBRARY_PATH', 'TEXINPUTS', 'GCC_COLORS', 'BOOST_ROOT',
                                      'RA_USE_BLAS']]))
variant_dir = env['variant_dir']

for var, default in [('CC', 'gcc'), ('CXX', 'g++'), ('FORTRAN', 'gfortran')]:
    ra.take_from_environ(env, var, default=default)
for var in ['FORTRANFLAGS', 'LINKFLAGS', 'CCFLAGS', 'CXXFLAGS']:
    ra.take_from_environ(env, var, wrapper=lambda x: x.split())
for var in ['RPATH', 'LIBPATH']:
    ra.take_from_environ(env, var, wrapper=lambda x: [x])

arch = os.popen('../config/config.guess').read()
cppcomp = os.popen('%s --version' % env['CXX']).read()

if (arch.find('apple-darwin') >= 0) and (cppcomp.find('g++') >= 0):
    archflags=['-march=native', '-Wa,-q']
else:
    archflags=['-march=native']

env.Prepend(CPPPATH=['..', '.'],
            CCFLAGS=archflags if str(env['CCFLAGS']).strip()=='' else '',
            CXXFLAGS=ra.CXXFLAGS,
            LINKFLAGS=ra.LINKFLAGS)

tester = ra.to_test_ra(env, variant_dir)

[tester(test)
 for test in ['at', 'bench', 'big-0', 'big-1', 'bug83', 'cellrank', 'checks', 'compatibility',
              'concrete', 'const', 'constexpr', 'dual', 'explode-0', 'foreign', 'frame-new',
              'frame-old', 'fromb', 'fromu', 'headers', 'io', 'iota', 'iterator-small', 'len',
              'list9', 'macros', 'mem-fn', 'ndebug', 'nested-0', 'old', 'operators', 'optimize',
              'owned', 'ownership', 'ply', 'ra-0', 'ra-1', 'ra-10', 'ra-11', 'ra-12', 'ra-13',
              'ra-14', 'ra-15', 'ra-2', 'ra-3', 'ra-4', 'ra-5', 'ra-6', 'ra-7', 'ra-8', 'ra-9',
              'ra-dual', 'reduction', 'reduction-1', 'reexported', 'reshape', 'return-expr',
              'self-assign', 'sizeof', 'small-0', 'small-1', 'stl-compat', 'swap', 'tensorindex',
              'test', 'tuples', 'types', 'vector-array', 'view-ops', 'wedge', 'where', 'wrank',
              # 'bug90745', # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90745 gcc 9-10.2 so far
              # 'end'
              ]]

tester('early', target='early-no-su', cppdefines={'RA_STATIC_UNROLL': '0'})
tester('early', target='early-su', cppdefines={'RA_STATIC_UNROLL': '1'})

tester('ra-10', target='ra-10a', cxxflags=['-O3'], cppdefines={'RA_DO_CHECK': '0'})
tester('ra-10', target='ra-10b', cxxflags=['-O1'], cppdefines={'RA_DO_CHECK': '0'})
tester('ra-10', target='ra-10c', cxxflags=['-O3'], cppdefines={'RA_DO_CHECK': '1'})
tester('ra-10', target='ra-10d', cxxflags=['-O1'], cppdefines={'RA_DO_CHECK': '1'})

if 'skip_summary' not in top:
    atexit.register(lambda: ra.print_summary(GetBuildFailures, 'ra/test'))
